﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Data.Common;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Business.Commands.SessionCommands;
using System.Web.Security;
using ZLCMS.Business.Util;
using DotNet.Common.Command;
using ZLCMS.Business.Commands.SessionCommands.User;


namespace ZLCMS.Web.MemberUser
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.Session[GlobalSessionString.CurrentMemberAccount] != null)
            {
                Response.Redirect("/MemberUser/Default.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Forms身份验证初始化 
            FormsAuthentication.Initialize();

            ClientScriptManager csm = this.Page.ClientScript;

            try
            {
                string lastPageUrl = (Session[GlobalSessionString.LastPageUrl] ?? "").ToString();

                ICommandResult<LoginResult> res = new LoginCommand().Execute(new LoginParameter() { LoginId = tb_id.Text.Trim(), Password = tb_pw.Text.Trim() });

                if (!res.Error)
                {
                    if (res.Result.Member.Status == "0")
                    {
                        csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('用户尚未激活！');", true);
                    }
                    else
                    {
                        //转发到请求的页面
                        if (string.IsNullOrEmpty(lastPageUrl))
                        {
                            Response.Redirect("/MemberUser/Default.aspx");
                        }
                        else
                        {
                            Response.Redirect(lastPageUrl);
                        }
                    }
                }
                else
                {
                    csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('用户名或密码错误！身份验证失败！');", true);
                }
            }
            catch (System.Exception ex)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('请先退出其他登录账号，身份验证失败！');", true);
            }
        }
    }
}